package edu.scnu.fans.fingertip.common.web;


import edu.scnu.fans.fingertip.common.api.ApiResponse;
import edu.scnu.fans.fingertip.common.api.RowResponse;
import org.springframework.util.Assert;

/**
 * ApiResponse 工具类
 */
public class ApiResponseUtils {


    /**
     * 如果code是ok 返回data，否则抛出异常
     */
    public static <T> T codeOkGetData(ApiResponse<T> apiResponse) {
        Assert.notNull(apiResponse, "apiResponse must not null");

        if (apiResponse.getCode() != 200) {
            throw new BusinessException(apiResponse.getMsg());
        }
        return apiResponse.getData();
    }

	public static <T> T codeOkGetRows(ApiResponse<RowResponse.RowData<T>> apiResponse) {
		Assert.notNull(apiResponse, "apiResponse must not null");

		if (apiResponse.getCode() != 200) {
			throw new BusinessException(apiResponse.getMsg());
		}
		return apiResponse.getData().getRows();
	}

}
